<?php

$dirfile = str_replace("\\", "/", str_replace("modulesetup", "", __DIR__));
$databasename = addslashes(trim($_POST["databasename"]));
$usernamedb = addslashes(trim($_POST["usernamedb"]));
$password = addslashes(trim($_POST["password"]));
$host = addslashes(trim($_POST["host"]));
$url = addslashes(trim($_POST["url"]));
if ($databasename == "" || $usernamedb == "" || $url == "") {
    echo "0";
    die;
}
$checkurl = '^http://(.*)$';
if (ereg($checkurl, $url) == false) {
    echo "0";
    die;
} else {
    $checkurl2 = '^http://(.*)/$';
    if (ereg($checkurl2, $url)) {
        $url = substr($url, 0, -1);
    }
}
$connect = new mysqli($host, $usernamedb, $password, $databasename);
if (@$connect->connect_error) {
    echo "0";
} else {
    configuser($usernamedb, $password, $dirfile);
    configdatabse($databasename, $host, $dirfile);
    configurl($url, $dirfile);
    echo "1";
    $connect->close();
}
die;

function configuser($username, $password, $dirfile) {
    $file = $dirfile . "config/autoload/local.php";
    $configuse = file($file);
    $flag = 0;
    $configtext = "";
    foreach ($configuse as $line => $linetext) {
        if ($flag == 1) {
            $configtext.="'username' => '" . $username . "',\n";
            $flag = 2;
        } else if ($flag == 2) {
            $configtext.="'password' => '" . $password . "',\n";
            $flag = 0;
        } else {
            $configtext.=$linetext;
        }
        if (strlen(strstr($linetext, "//inforuse")) > 0) {
            $flag = 1;
        }if (strlen(strstr($linetext, "//endinforuse")) > 0) {
            $flag = 0;
        }
    }
    $filew = fopen($file, "w");
    fwrite($filew, $configtext);
    fclose($filew);
}

function configdatabse($databasename, $host, $dirfile) {
    $file = $dirfile . "config/autoload/global.php";
    $configuse = file($file);
    $flag = 0;
    $configtext = "";
    foreach ($configuse as $line => $linetext) {
        if ($flag == 1) {
            $configtext.="'dsn' => 'mysql:dbname=$databasename;host=$host',\n";
            $flag = 0;
        } else {
            $configtext.=$linetext;
        }
        if (strlen(strstr($linetext, "//infordatabase")) > 0) {
            $flag = 1;
        }if (strlen(strstr($linetext, "//endinfordatabase")) > 0) {
            $flag = 0;
        }
    }
    $filew = fopen($file, "w");
    fwrite($filew, $configtext);
    fclose($filew);
}

function configurl($url, $dirfile) {
    $file = $dirfile . "index.php";
    $configuse = file($file);
    $flag = 0;
    $configtext = "";
    foreach ($configuse as $line => $linetext) {
        if ($flag == 1) {
            $configtext.="define('WEB_PATH', '$url');\n";
            $flag = 2;
        } else if ($flag == 2) {
            $configtext.="define('WEB_PATH_HEADER', '$url/public/');\n";
            $flag = 0;
        } else {
            $configtext.=$linetext;
        }
        if (strlen(strstr($linetext, "//inforurl")) > 0) {
            $flag = 1;
        }if (strlen(strstr($linetext, "//endinforurl")) > 0) {
            $flag = 0;
        }
    }
    $filew = fopen($file, "w");
    fwrite($filew, $configtext);
    fclose($filew);
}
